package com.jy.controller.filemanager;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jy.common.ajax.AjaxRes;
import com.jy.common.mybatis.Page;
import com.jy.common.utils.base.Const;
import com.jy.controller.base.BaseController;
import com.jy.entity.filemanager.TSUser;
import com.jy.service.filemanager.user.TuserService;
import com.jy.task.fileoper.TcpOper;

@Controller
@RequestMapping("/tuser")
public class TuserController extends BaseController<TSUser> {

	@Autowired
	private TuserService service;

	/**
	 * @api {post} /tuser/findBypage  用户分页列表查询
	 * 
	 * @apiName findBypage
	 * @apiGroup tuser
	 * @apiParam {Object} Page<TSProduct> 分页对象
	 * @apiSuccess {Object} obj 1.resMsg 2.obj
	 * @apiParam {Object} TSProduct  用户对象
	 */
	@RequestMapping(value = "findBypage", method = RequestMethod.POST)
	@ResponseBody
	public AjaxRes index(Page<TSUser> page, TSUser o) {
		AjaxRes ar = getAjaxRes();
		try {
			Page<TSUser> result = service.findByPage(o, page);
			Map<String, Object> p = new HashMap<String, Object>();
			p.put("list", result);
			ar.setSucceed(p);
		} catch (Exception e) {
			logger.error(e.toString(), e);
			ar.setFailMsg(Const.DATA_FAIL);
		}
		return ar;
	}

	/**
	 * @api {post} /tuser/find  用户列表查询
	 * 
	 * @apiName find
	 * @apiGroup tuser
	 * @apiParam {Object} Page<TSProduct> 分页对象
	 * @apiSuccess {Object} obj 1.resMsg 2.obj
	 * @apiParam {Object} TSProduct  用户对象
	 */
	@RequestMapping(value = "find", method = RequestMethod.POST)
	@ResponseBody
	public AjaxRes find(TSUser o) {
		AjaxRes ar = getAjaxRes();
		try {
			TSUser obj = service.findUserById(o.getId());
			ar.setSucceed(obj);
		} catch (Exception e) {
			logger.error(e.toString(), e);
			ar.setFailMsg(Const.DATA_FAIL);
		}
		return ar;
	}

	@RequestMapping(value = "checkName", method = RequestMethod.POST)
	@ResponseBody
	public AjaxRes checkName(String name) {
		AjaxRes ar = getAjaxRes();
		try {
			List<TSUser> objList = service.getUserListByName(name);
			if (objList.size() > 0) {
				ar.setObj(true);
			} else {
				ar.setObj(false);
			}
		} catch (Exception e) {
			logger.error(e.toString(), e);
			ar.setFailMsg(Const.SAVE_FAIL);
		}
		return ar;
	}

	@RequestMapping(value = "getPrincipal", method = RequestMethod.POST)
	@ResponseBody
	public AjaxRes getPrincipal() {
		AjaxRes ar = getAjaxRes();
		try {
			List<TSUser> obj = service.getPrincipal();
			ar.setObj(obj);
		} catch (Exception e) {
			logger.error(e.toString(), e);
			ar.setFailMsg(Const.SAVE_FAIL);
		}
		return ar;
	}

	/**
	 * @api {post} /tuser/add  用户新增
	 * 
	 * @apiName add
	 * @apiGroup tuser
	 * @apiParam {Object} Page<TSProduct> 分页对象
	 * @apiSuccess {Object} obj 1.resMsg 2.obj
	 * @apiParam {Object} TSProduct  用户对象
	 */
	@RequestMapping(value = "add", method = RequestMethod.POST)
	@ResponseBody
	public AjaxRes add(TSUser o) {
		AjaxRes ar = getAjaxRes();
		try {
			o.setId(get32UUID());
			int res = service.creatUser(o);
			if (res == 1)
				ar.setSucceedMsg(Const.SAVE_SUCCEED);
			else
				ar.setFailMsg(Const.SAVE_FAIL);
		} catch (Exception e) {
			logger.error(e.toString(), e);
			ar.setFailMsg(Const.SAVE_FAIL);
		}
		return ar;
	}

	/**
	 * @api {post} /tuser/update  用户更新
	 * 
	 * @apiName update
	 * @apiGroup tuser
	 * @apiParam {Object} Page<TSProduct> 分页对象
	 * @apiSuccess {Object} obj 1.resMsg 2.obj
	 * @apiParam {Object} TSProduct  用户对象
	 */
	@RequestMapping(value = "update", method = RequestMethod.POST)
	@ResponseBody
	public AjaxRes update(TSUser o) {
		AjaxRes ar = getAjaxRes();
		try {
			int res = service.updateUser(o);
			if (res == 1)
				ar.setSucceedMsg(Const.UPDATE_SUCCEED);
			else
				ar.setFailMsg(Const.UPDATE_FAIL);
		} catch (Exception e) {
			logger.error(e.toString(), e);
			ar.setFailMsg(Const.UPDATE_FAIL);
		}
		return ar;
	}

	/**
	 * @api {post} /tuser/del  用户删除
	 * 
	 * @apiName del
	 * @apiGroup tuser
	 * @apiParam {Object} Page<TSProduct> 分页对象
	 * @apiSuccess {Object} obj 1.resMsg 2.obj
	 * @apiParam {Object} TSProduct  用户对象
	 */
	@RequestMapping(value = "del", method = RequestMethod.POST)
	@ResponseBody
	public AjaxRes del(TSUser o) {
		AjaxRes ar = getAjaxRes();
		try {
			int res = service.deleteUser(o);
			if (res == 1)
				ar.setSucceedMsg(Const.DEL_SUCCEED);
			else
				ar.setFailMsg(Const.DEL_FAIL);
		} catch (Exception e) {
			logger.error(e.toString(), e);
			ar.setFailMsg(Const.DEL_FAIL);
		}
		return ar;
	}

	/**
	 * @api {post} /tuser/connectTcp  是否能连接tcp
	 * @apiName connectTcp
	 * @apiGroup tUser
	 * @apiSuccess {Boolean} obj
	 * @apiParam {Object} TSProduct  产品对象
	 */
	@RequestMapping(value = "connectTcp", method = RequestMethod.POST)
	@ResponseBody
	public AjaxRes connectTcp(TSUser o) {
		AjaxRes ar = getAjaxRes();
		boolean result = false;
		try {
			String newIpString = o.getAddress();
			if (o.getAddress().indexOf("/") > 0) {
				newIpString = o.getAddress().substring(0,
						o.getAddress().indexOf("/"));
			}
			TcpOper objOper = new TcpOper(newIpString, o.getServerName(),
					o.getServerPassword(), o.getPort());
			result = objOper.tcpConnect();
		} catch (Exception e) {
		}
		ar.setObj(result);
		return ar;
	}
}
